import os

import yaml
def getdata(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        return yaml.safe_load(f)

import yagmail
def send_mail(filepath):
    mail = yagmail.SMTP(user='3210993712@qq.com', password="liu200412", host="smtp.qq.com")
    mail.send(to='3210993712@qq.com',subject="电商系统后台测试报告",contents="请领导查看", attachments=filepath)


import yaml
import logging
import time
from util.basedir import LOG_DIR
from logging.handlers import RotatingFileHandler


def get_yaml_data(file_path):
    """读取YAML数据（用于数据驱动）"""
    with open(file_path, "r", encoding="utf-8") as f:
        return yaml.safe_load(f)


def init_logger():
    """初始化日志配置（按考核要求采集日志）"""
    # 日志文件名（按时间命名）
    log_file = os.path.join(LOG_DIR, f"ui_test_{time.strftime('%Y%m%d')}.log")

    # 日志格式
    log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    formatter = logging.Formatter(log_format)

    # 控制台 handler
    console_handler = logging.StreamHandler()
    console_handler.setFormatter(formatter)
    console_handler.setLevel(logging.INFO)

    # 文件 handler（轮转日志，最大100MB，保留5个备份）
    file_handler = RotatingFileHandler(
        log_file, maxBytes=1024 * 1024 * 100, backupCount=5, encoding="utf-8"
    )
    file_handler.setFormatter(formatter)
    file_handler.setLevel(logging.DEBUG)

    # 配置日志器
    logger = logging.getLogger("web_ui_test")
    logger.setLevel(logging.DEBUG)
    logger.addHandler(console_handler)
    logger.addHandler(file_handler)

    return logger


# 初始化日志对象（全局可用）
logger = init_logger()